package com.qijia.common.utils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:分页组件
 * 
 * @version:
 * 
 */
public class PageUtil {
	/**
	 * 每页记录数，默认值为10条
	 */
	private int pageSize = 10;
	/**
	 * 当前页码,默认为第一页
	 */
	private int curPage = 1;
	/**
	 * 总记录数
	 */
	private int rowCnt = 0;
	/**
	 * 总页数
	 */
	@SuppressWarnings("unused")
	private int pageCnt = 0;
	/**
	 * 每页的记录列表
	 */
	private List<?> pageList;
	/**
	 * ibatis count sql mapped id
	 */
	private String countMappedId = "countByExample";

	/**
	 * ibatis select sql mapped id
	 */
	private String selectDataMappedId = "selectByExample";

	/**
	 * 链接地址
	 */
	private String rootPath;
	/**
	 * 分页条url参数Map
	 */
	private Map<String, Object> pageBarParamMap = null;
	/**
	 * 20,显示页数链接
	 */
	private int listStep = 20;
	private String stepBar;

	private Object sqlParamObj;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if (pageSize > 0)
			this.pageSize = pageSize;
	}

	public int getCurPage() {
		if (this.getPageCnt() > 0 && this.getPageCnt() < this.curPage)
			this.curPage = this.getPageCnt();
		if (this.curPage < 1)
			this.curPage = 1;
		return curPage;
	}

	public void setCurPage(int curPage) {
		if (curPage > 0)
			this.curPage = curPage;
	}

	public int getRowCnt() {
		return rowCnt;
	}

	public void setRowCnt(int rowCnt) {
		this.rowCnt = rowCnt;
	}

	public int getPageCnt() {
		return (rowCnt % pageSize == 0) ? (rowCnt / pageSize) : (rowCnt
				/ pageSize + 1);
	}

	public void setPageCnt(int pageCnt) {
		this.pageCnt = pageCnt;
	}

	public int getStartRow() {
		return (getCurPage() - 1) * pageSize;
	}

	public int getEndRow() {
		return getCurPage() * pageSize;
	}

	public List<?> getPageList() {
		return pageList;
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public Map<String, Object> getPageBarParamMap() {
		return pageBarParamMap;
	}

	public void setPageBarParamMap(Map<String, Object> pageBarParamMap) {
		this.pageBarParamMap = pageBarParamMap;
	}

	public Object getSqlParamObj() {
		return sqlParamObj;
	}

	public void setSqlParamObj(Object sqlParamObj) {
		this.sqlParamObj = sqlParamObj;
	}

	/**
	 * 转换查询参数为input隐藏输入框
	 * 
	 * @return
	 */
	public String paramHiddenInput() {
		if (this.getPageBarParamMap() == null)
			return "";

		String paramStr = "";
		/**
		 * 循环paramMap,转换查询参数为url串
		 */
		for (Object key : pageBarParamMap.keySet()) {
			if (key != null && pageBarParamMap.get(key) != null)
				paramStr += "<input type=\"hidden\" name=\"" + key.toString()
						+ "\" value=\"" + pageBarParamMap.get(key) + "\"/>";

		}

		return paramStr;

	}

	/**
	 * 转换查询参数Map为字符串组合<原始的>
	 * 
	 * @return
	 */
	public String paramStr() {
		if (this.getPageBarParamMap() == null)
			return "";

		String paramStr = "";
		/**
		 * 循环paramMap,转换查询参数为url串
		 */
		for (Object key : pageBarParamMap.keySet()) {
			if (key != null && pageBarParamMap.get(key) != null)
				paramStr += "&" + key.toString() + "="
						+ pageBarParamMap.get(key);

		}
		// if(!paramStr.trim().equals(""))
		// paramStr=this.getRootPath()+paramStr;

		return paramStr;
	}

	/**
	 * 
	 * 方法描述： 转换查询参数Map为字符串组合<url使用了urlrewriter，所以这里必须符合规则> 创建时间：Mar 26, 2010
	 * 10:46:46 AM
	 * 
	 * @return
	 */
	public String paramStrStatic() {
		if (this.getPageBarParamMap() == null)
			return "";

		String paramStr = "";
		/**
		 * 循环paramMap,转换查询参数为url串
		 */
		for (Object key : pageBarParamMap.keySet()) {
			if (key != null && pageBarParamMap.get(key) != null)
				if (key.equals("act"))
					paramStr += "/" + pageBarParamMap.get(key);
				else
					paramStr += "/" + key.toString() + "/"
							+ pageBarParamMap.get(key);

		}
		// if(!paramStr.trim().equals(""))
		// paramStr=this.getRootPath()+paramStr;
		return paramStr;
	}

	public void setPageList(List<?> pageList) {
		this.pageList = pageList;
	}

	public int getListStep() {
		return listStep;
	}

	public void setListStep(int listStep) {
		this.listStep = listStep;
	}

	/**
	 * 
	 * 方法描述： 基本的分页条【首页，上页，下页，尾页】 创建时间：Apr 14, 2010 2:18:23 PM
	 * 
	 * @return
	 */
	public String getSimpleBar() {
		if (getPageCnt() < 1)
			return "";

		StringBuffer sb = new StringBuffer();
		sb.append("共" + getRowCnt() + " 条&nbsp; 当前" + getCurPage() + "/"
				+ getPageCnt() + "页&nbsp;&nbsp;&nbsp;&nbsp;");
		if (getCurPage() > 1) {
			sb
					.append("<a href=\"javascript:PaginatedTools.firstPage('1')\" > <font color=\"#000000\">首页</font></a>&nbsp;&nbsp;");
			sb
					.append("<a href=\"javascript:PaginatedTools.prePage('"
							+ getCurPage()
							+ "')\" ><font color=\"#000000\">上一页</font></a>&nbsp;&nbsp;");
		}

		if (getCurPage() < getPageCnt()) {
			sb
					.append("<a href=\"javascript:PaginatedTools.nextPage('"
							+ getCurPage()
							+ "','"
							+ getPageCnt()
							+ "')\" ><font color=\"#000000\">下一页</font></a>&nbsp;&nbsp;");
			sb
					.append("<a href=\"javascript:PaginatedTools.lastPage('"
							+ getCurPage()
							+ "','"
							+ getPageCnt()
							+ "')\" ><font color=\"#000000\">尾页</font></a>&nbsp;&nbsp;");
		}

		sb
				.append("&nbsp;&nbsp;转到第&nbsp;&nbsp;<input name=\"jumpno\" id=\"jumpno\" type=\"text\" class=\"input\" size=\"2\" />&nbsp;&nbsp;页&nbsp;&nbsp;<input class=\"SchBtn1 fRi\" title=\"GO\" type=\"button\" value=\"GO\" onclick=\"PaginatedTools.jumpWithIdElem('jumpno',"
						+ getCurPage() + "," + getPageCnt() + ")\"/>");

		sb.append(paramHiddenInput());
		return sb.toString();
	}

	/**
	 * 
	 * 方法描述： 页码分页条【上页，1,2,3,4,5,6,下页】 创建时间：Apr 14, 2010 2:19:14 PM
	 * 
	 * @return
	 */
	public String getStepBar() {
		if (getPageCnt() < 1)
			return "";
		StringBuffer sb = new StringBuffer();
		// int pagescount = (int) Math.ceil((double) this.rowCnt /
		// this.pageSize);//求总页数，ceil（num）取整不小于num
		if (getPageCnt() < getCurPage()) {
			this.curPage = getPageCnt();// 如果分页变量大总页数，则将分页变量设计为总页数
		}
		if (getCurPage() < 1) {
			this.curPage = 1;// 如果分页变量小于１,则将分页变量设为１
		}
		int listbegin = (getCurPage() - (int) Math
				.ceil((double) getListStep() / 2));// 从第几页开始显示分页信息
		if (listbegin < 1) {
			listbegin = 1;
		}
		int listend = getCurPage() + getListStep() / 2;// 分页信息显示到第几页
		if (listbegin < getListStep() / 2)
			listend = getCurPage() + getListStep();
		if (listend > getPageCnt()) {
			listend = getPageCnt() + 1;
		}
		// <显示分页信息
		// <显示上一页
		if (getCurPage() > 1) {
			sb.append("<a href=" + this.getRootPath() + "?page="
					+ (getCurPage() - 1) + "" + paramStr() + ">上一页</a>");
			sb.append("&nbsp;");
		}// >显示上一页
		// <显示分页码
		for (int i = listbegin; i < listend; i++) {
			if (i != getCurPage()) {// 如果i不等于当前页
				sb.append("<a href=" + this.getRootPath() + "?page=" + i + ""
						+ paramStr() + ">" + i + "</a>");
			} else {
				sb.append("<span class=\"current\">" + i + "</span>");
			}
			sb.append("&nbsp;");
		}// 显示分页码>
		// <显示下一页
		if (getCurPage() != getPageCnt()) {
			sb.append("<a href=" + this.getRootPath() + "?page="
					+ (getCurPage() + 1) + "" + paramStr() + ">下一页</a>");
		}// >显示下一页
		// >显示分页信息
		stepBar = sb.toString();

		return stepBar;

	}

	/**
	 * 
	 * 方法描述： 页码分页条【上页，1,2,3,4,5,6,下页】<url使用了urlrewriter，所以这里必须符合规则> 创建时间：Mar
	 * 26, 2010 3:49:54 PM
	 * 
	 * @return
	 */
	public String getStepBarStatic() {
		if (getPageCnt() < 1)
			return "";
		StringBuffer sb = new StringBuffer();
		// int pagescount = (int) Math.ceil((double) this.rowCnt /
		// this.pageSize);//求总页数，ceil（num）取整不小于num
		if (getPageCnt() < getCurPage()) {
			this.curPage = getPageCnt();// 如果分页变量大总页数，则将分页变量设计为总页数
		}
		if (getCurPage() < 1) {
			this.curPage = 1;// 如果分页变量小于１,则将分页变量设为１
		}
		int listbegin = (getCurPage() - (int) Math
				.ceil((double) getListStep() / 2));// 从第几页开始显示分页信息
		if (listbegin < 1) {
			listbegin = 1;
		}
		int listend = getCurPage() + getListStep() / 2;// 分页信息显示到第几页
		if (listbegin < getListStep() / 2)
			listend = getCurPage() + getListStep();
		if (listend > getPageCnt()) {
			listend = getPageCnt() + 1;
		}
		// <显示分页信息
		// <显示上一页
		if (getCurPage() > 1) {
			sb.append("<a href=" + paramStrStatic() + "/curPage/"
					+ (getCurPage() - 1) + ".htm>上一页</a>");
			sb.append("&nbsp;");
		}// >显示上一页
		// <显示分页码
		for (int i = listbegin; i < listend && (listend - listbegin) > 1; i++) {
			if (i != getCurPage()) {// 如果i不等于当前页
				sb.append("<a href=" + paramStrStatic() + "/curPage/" + i
						+ ".htm>[" + i + "]</a>");
			} else {
				sb.append("[" + i + "]");
			}
			sb.append("&nbsp;");
		}// 显示分页码>
		// <显示下一页
		if (getCurPage() != getPageCnt()) {
			sb.append("<a href=" + paramStrStatic() + "/curPage/"
					+ (getCurPage() + 1) + ".htm>下一页</a>");
		}// >显示下一页
		// >显示分页信息
		stepBar = sb.toString();

		return stepBar;

	}

	public static void main(String[] args) {
		int listbegin = (3 - (int) Math.ceil((double) 5 / 2));// 从第几页开始显示分页信息

		int listend = 3 + 5 / 2;// 分页信息显示到第几页
		System.out.println(listbegin + " >> " + listend);
	}

	public String getCountMappedId() {
		return countMappedId;
	}

	public void setCountMappedId(String countMappedId) {
		this.countMappedId = countMappedId;
	}

	public String getSelectDataMappedId() {
		return selectDataMappedId;
	}

	public void setSelectDataMappedId(String selectDataMappedId) {
		this.selectDataMappedId = selectDataMappedId;
	}

	public static PageUtil newInstance() throws InstantiationException,
			IllegalAccessException {
		return PageUtil.class.newInstance();
	}
	
	public String toString() {
		Method[] ms = this.getClass().getDeclaredMethods();
		String s = "";
		for (Method m : ms) {
			if (m.getName().indexOf("get") != 0)
				continue;
			try {
				s += m.getName() + "-->"
						+ ObjectUtils.invokeMethod(this, m.getName()) + " \n ";
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return s;
	}

}
